package com.tianqu.tms.core.base;

import java.sql.Connection;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.ConnectionException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;

/**
 * 
 * @author liugang
 *
 */
public class TxController {

	/* Log */
	private Logger logger = Logger.getLogger(TxController.class);

	/**
	 * myTxController
	 */
	public static ThreadLocal myTxController = new ThreadLocal();
	
	/* connection */
	private Connection conn = null;

	/**
	 * 
	 * TxController
	 */
	public TxController() {

	}

	/**
	 * get
	 * 
	 * @return TxController
	 */
	public static TxController getTxController() {
		TxController txc = (TxController) myTxController.get();
		if (txc == null) {
			txc = new TxController();
			myTxController.set(txc);
		}
		return txc;
	}

	/**
	 * getConnection
	 * 
	 * @return Connection
	 * @throws TransactionException 
	 * @throws ClassNotFoundException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */
	public Connection getConnection() throws TransactionException {
		try {
			if (conn == null || conn.isClosed()) {
				conn = getConnection(true);
			}
			return conn;
		} catch (SQLException e) {
			logger.error("getConnection error" + e);
			throw new TransactionException("Connection Error", e);
		}
	}

	/**
	 * getConnection
	 * 
	 * @param create 
	 * @return Connection
	 * @throws TransactionException 
	 * @throws ClassNotFoundException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 *  
	 */
	public Connection getConnection(boolean create) throws TransactionException  {
		if (create) {
			try {
				return getNewConnection();
			} catch (ConnectionException e) {
				throw new TransactionException(e);
			}
		}
		return getConnection();
	}

	/**
	 * getNewConnection
	 * 
	 * @return Connection
	 * @throws ConnectionException 
	 * @throws ClassNotFoundException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */
	private Connection getNewConnection() throws ConnectionException  {
		return ConnectionFactory.getInstance().getConnection();
	}

	/**
	 * begin
	 * 
	 * @throws Exception
	 */
	public void begin() throws TransactionException {
		
	}

	/**
	 * commit
	 * @throws TransactionException 
	 * 
	 * @throws Exception
	 */
	public void commit() throws  TransactionException {
		try {
			if (conn != null && !conn.getAutoCommit()) {
				conn.commit();
			}
		} catch (SQLException sqle) {
			logger.error("TxController commit# " + "DB submit 失败" + sqle);
			throw new TransactionException(TmsException.DB_TXCRROR, sqle);

		}finally{
			try {
				myTxController.remove();
				if ( conn != null && !conn.isClosed()){conn.close();}
			} catch (SQLException e) {
				logger.error("TxController commit# " + "DB close 失败" + e);
			}
			conn = null; 
		}
	}

	/**
	 * rollback
	 * 
	 * @throws Exception
	 */
	public void rollback() throws TransactionException {

		try {
			if (conn != null) {
				conn.rollback();
			}
		} catch (SQLException sqle) {
			logger.error("TxController rollback# " + "DB rollback 失败"	+ sqle);
			throw new TransactionException(TmsException.DB_TXCRROR, sqle);
		}finally{
			try {
				myTxController.remove();
				if ( conn != null && !conn.isClosed()){conn.close();}
			} catch (SQLException sqle) {
				logger.error("TxController rollback# " + "DB rollback 后，connect close 失败"
						+ sqle);
				throw new TransactionException("Connection Error", sqle);
			}
			conn = null; 
		}

	}

}